
# Do commit

require 'net/http'
require 'uri'
require 'json'


def add_command (url, data)
    url = URI.parse(url)  
    req = Net::HTTP::Post.new(url.path, {'Content-Type' => 'application/json'})  
    req.body = data  
    res = Net::HTTP.new(url.host, url.port).start{|http| http.request(req)}  

    puts res.body
end

# this API command/add for DataCenter (Not for Agent)
# A command is against to a certain Agent client. (So JSON contains station field)
#  
#
#
#

data = <<COMMAND_TO_ADD
{
	"station": "station id",
	"token": "abcdefg",
	"cmd":{
		"station" : "station-rb-test",
		"cmd": "history-data",
		"arg": "[{\\"start_time\\":\\"XXXX-XX-XX\\"},{\\"stop_time\\":\\"XXXX-XX-XX\\"}]"
	}
}
COMMAND_TO_ADD


begin
	# Test API data/commit
	#
	p JSON.parse(data)
	add_command("http://127.0.0.1/cmd/add", data)
rescue JSON::ParserError
	puts "Check your Json Data format."
	puts data
rescue Exception => e
	puts e
end
